package com.mySampleApplication.client.codeRule;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.TreeGrid;

/**
 * 用法
 * // 创建按钮和事件处理
 * Button addButton = new Button("Add");
 * addButton.setShowRollOver(true);
 * addButton.setShowDown(true);
 * addButton.addClickHandler(event -> showAddDialog(listGrid));
 * private static void showAddDialog(ListGrid listGrid) {
 * Window dialog=SShowWindow.getShowWindow();
 * dialog.show();
 * }
 */
public class CkscWindow {
    public static Window getShowWindow(int width, int height, String title, final ListGridRecord record, ListGrid listGrid) {
        final Window winModal = new Window();
        //winModal.setAutoSize(true);
        winModal.setWidth(width);
        winModal.setHeight("100%");
        winModal.setTitle(title);
        // 设置窗口的位置
        winModal.setLeft(0);
        winModal.setTop(0);
        winModal.setShowMinimizeButton(false);
        winModal.setIsModal(true);
        winModal.setShowModalMask(true);
        winModal.setCanDragReposition(true);

        winModal.addCloseClickHandler(new CloseClickHandler() {
            public void onCloseClick(CloseClickEvent event) {
                winModal.destroy();
            }
        });

        HLayout mainLayout = new HLayout();
        mainLayout.setWidth100();
        mainLayout.setHeight100();

        Label navigationLabel = new Label();
        //navigationLabel.setContents("Navigation");
        navigationLabel.setAlign(Alignment.CENTER);
        navigationLabel.setOverflow(Overflow.HIDDEN);
        navigationLabel.setWidth("20%");
        navigationLabel.setShowResizeBar(true);
        navigationLabel.addChild(getMleft());

        mainLayout.addMember(navigationLabel);

        VLayout vLayout = new VLayout();
        vLayout.setWidth("80%");

//        Label listingLabel = new Label();
//        listingLabel.setContents("Listing");
//        listingLabel.setAlign(Alignment.CENTER);
//        listingLabel.setOverflow(Overflow.HIDDEN);
//        listingLabel.setHeight("30%");
//        listingLabel.setShowResizeBar(true);

        Label detailsLabel = new Label();
        detailsLabel.setAlign(Alignment.CENTER);
        detailsLabel.setOverflow(Overflow.HIDDEN);
        detailsLabel.setHeight("80%");
        detailsLabel.addChild(getContent());
        //vLayout.addMember(listingLabel);
        vLayout.addMember(detailsLabel);

        mainLayout.addMember(vLayout);
        winModal.addItem(mainLayout);

        Button cancelButton = new Button("Cancel");
        cancelButton.addClickHandler(event -> winModal.hide());

        HLayout buttonLayout = new HLayout();
        buttonLayout.setMembersMargin(10);
        buttonLayout.setAlign(Alignment.RIGHT);
        buttonLayout.setWidth100();
        buttonLayout.setHeight(30);
        buttonLayout.setPadding(5);
        buttonLayout.setLayoutAlign(VerticalAlignment.BOTTOM);
//        buttonLayout.addMembers(cancelButton);
        winModal.addItem(buttonLayout);

        HStack layoutTopRow = new HStack(10);
        layoutTopRow.setHeight(50);

        VLayout layoutMain = new VLayout();
        layoutMain.addMember(layoutTopRow);

        layoutMain.draw();
        return winModal;
    }

    public static HTMLFlow getContent(){
        String str="内容组装";
        HTMLFlow htmlFlow = new HTMLFlow();
        htmlFlow.setContents(str);
        htmlFlow.setPadding(10);
        return htmlFlow;
    }

    public static TreeGrid getMleft(){
        String str="编码名称";
        HTMLFlow htmlFlow = new HTMLFlow();
        htmlFlow.setContents(str);
        htmlFlow.setPadding(10);
        // 创建菜单树
        TreeGrid menuTree = new TreeGrid();
        menuTree.setWidth(200);
        menuTree.setHeight100();
        menuTree.setData(getMenuData());
        menuTree.addSelectionChangedHandler(new SelectionChangedHandler() {
            public void onSelectionChanged(SelectionEvent event) {
                // 当菜单项被选中时，刷新右边的内容区域
               // refreshContent();
            }
        });
        return menuTree;
    }
    private static ListGridRecord[] getMenuData() {
        ListGridRecord[] records = new ListGridRecord[3];

        ListGridRecord record1 = new ListGridRecord();
        record1.setAttribute("title", "菜单项1");
        records[0] = record1;

        ListGridRecord record2 = new ListGridRecord();
        record2.setAttribute("title", "菜单项2");
        records[1] = record2;

        ListGridRecord record3 = new ListGridRecord();
        record3.setAttribute("title", "菜单项3");
        records[2] = record3;

        return records;
    }
}
